package br.com.seosoft.simplecalcfinan.bancoDados;

import br.com.seosoft.simplecalcfinan.geral;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class SQLiteHelper extends SQLiteOpenHelper {

	private String[] scriptCreate;
	private String[] scriptDelete;
	private String[] updateData = new String[] { "ALTER TABLE [contas] ADD COLUMN [data_time] DATETIME DEFAULT null;","UPDATE [contas] SET [data_time] = date('now');" };

	/*
	 * public SQLiteHelper(Context context, String name, CursorFactory factory,
	 * int version) { super(context, name, factory, version); // TODO
	 * Auto-generated constructor stub }
	 */

	public SQLiteHelper(Context context, String nomeBanco, int versaoBanco,
			String[] scriptCreate, String[] scriptDelete) {
		super(context, nomeBanco, null, versaoBanco);
		Log.i(geral.LogCat, "SQLiteHelper construtor");

		this.scriptCreate = scriptCreate;
		this.scriptDelete = scriptDelete;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		Log.i(geral.LogCat, "SQLiteHelper.onCrate()");
		int numScripts = scriptCreate.length;
		for (int i = 0; i < numScripts; i++) {
			String sql = scriptCreate[i];
			Log.i(geral.LogCat, sql);
			db.execSQL(sql);

		}
	}




	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		Log.i(geral.LogCat, "SQLiteHelper onUpgrade()");
		Log.w(geral.LogCat, "Atualizando da versao " + oldVersion
				+ " para a versao " + newVersion
				+ ". o banco de dados sera alterado");

//		 int numScripts = scriptDelete.length;
//		 for (int i = 0; i < numScripts; i++) {
//		 Log.i(geral.LogCat, scriptDelete[i]);
//		 String sql = scriptDelete[i];
//		 Log.i(geral.LogCat, sql);
//		 db.execSQL(sql);
//		
//		 }

		Log.i(geral.LogCat, "SQLiteHelper version <=4");
		
		String sql;
		
		if (oldVersion <=4) {
			 sql = updateData[0];
			 try {
				db.execSQL(sql);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
			

		
	}
	
	

}
